package managers.dateManager.endFrozen.beginChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.TimeConstants;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.managers.TaskManager;
import org.tbjd.client.states.StateEndFrozenASAP;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p><b>
 * Case</b>
 * <p>Task with 2 predecessors and everyone in ASAP ChainingMode</p>
 * <p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * BeginDate of the task "task" is changed
 * </p>
 * <b>Expected</b>
 * <ul>
 * <li>BeginDate "task" changed</li>
 * <li>EndDate "task" didn't change</li>
 * <li>Duration "task" changed</li>
 * <li>"task" chainingMode is set to Manual</li>
 * <li>Predecessors aren't changed</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class EndFrozen_BeginChange_UpdatePredecessors_Basic extends TestCase {

	/*
	 * Task		Begins 		Ends 		Predecessor Successor 	ChainingMode	task +2 days
	 * o p1		15.6.2009 	17.6.2009 	- 			task		ASAP 			15.6.2009 - 17.6.2009
	 * o p2		18.6.2009 	21.6.2009 	- 			task		ASAP 			18.6.2009 - 21.6.2009
	 * o task 	22.6.2009 	25.6.2009 	-	 		-		 	ASAP 			24.6.2009 - 25.6.2009
	 */

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task task = null;
	private TaskManager dateManager = new TaskManager();
	private static final long ADDED_DAYS = 2;

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		Task p1 = TaskFactory.getInstance();
		p1.setName("p1");
		p1.setBeginDate(df.parse("15.6.2009"));
		p1.setEndDate(df.parse("17.6.2009"));
		p1.setDuration(48);
		p1.setState(new StateEndFrozenASAP(p1.getState().getDurationModeState()));

		Task p2 = TaskFactory.getInstance();
		p2.setName("p2");
		p2.setBeginDate(df.parse("18.6.2009"));
		p2.setEndDate(df.parse("21.6.2009"));
		p2.setDuration(72);
		p2.setState(new StateEndFrozenASAP(p2.getState().getDurationModeState()));

		this.task = TaskFactory.getInstance();
		this.task.setName("task");
		this.task.setBeginDate(df.parse("22.6.2009"));
		this.task.setEndDate(df.parse("25.6.2009"));
		this.task.setDuration(72);
		this.task.setState(new StateEndFrozenASAP(task.getState().getDurationModeState()));

		p1.addSuccessor(this.task);
		p2.addSuccessor(this.task);

		super.setUp();
	}

	public void testReturnedBeanListNotNull() {
		System.out.println("");
		log.debug(" === testReturnedBeanListNotNull");
		log.debug("original BeginDate = " + df.format(this.task.getBeginDate()));
		log.debug("original EndDate = " + df.format(this.task.getEndDate()));

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

	}

	public void testReturnedListSizeIsOk() {
		System.out.println("");
		log.debug(" === testReturnedListSizeIsOk");

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("6.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 1", 1, updatedBeans.size());

	}

	public void testReturnedDatesAreOk() {
		System.out.println("");
		log.debug(" === testReturnedDatesAreOk");

		Date newBeginDate = new Date(task.getBeginDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));

		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()));
			try {
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("24.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("25.6.2009"), updatedBean.getEndDate());
				}

			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}
	}

	public void testBeanChangedChainingModeToManual(){
		System.out.println("");
		log.debug(" === testBeanChangedChainingModeToManual");

		Date newBeginDate = new Date(task.getBeginDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));

		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		
		assertFalse("Returned bean is in manual chaining mode", updatedBeans.get(0).getState().isASAPChainingMode());
	}
	
	public void testBeginDateException() {
		System.out.println("");
		log.debug(" === testBeginDateException");

		Date originalBeginDate = task.getBeginDate();
		Date originalEndDate = task.getEndDate();
		float originalDuration = task.getDuration();
		Date newBeginDate = new Date(task.getEndDate().getTime() + 2 * TimeConstants.DAY_IN_MILLIS);

		log.debug("original beginDate = " + originalBeginDate);
		log.debug("original endDate = " + originalEndDate);
		log.debug("original duration = " + originalDuration);
		log.debug("new BeginDate = " + newBeginDate);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, task);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("compute with new BeginDate = " + newBeginDate + "...");
		try {
			dateManager .manageDates(this.map);
			fail();

		} catch (TaskManagerException e) {
			assertNotNull(e.getMessage());
			
		}
		
	}
}